package ws.very.util.lang

import scala.annotation.tailrec
@deprecated//Iterator.continually(Random.nextInt())
object LoopColl {


  final  def apply[T](getAll: => Iterator[T])(waitTime: Int = 0, beforeWaitFn: => Unit = {}) =
    new Iterator[T] {
      var all: Iterator[T] = getAll
      def hasNext = true
      
      def next = synchronized {
        try {
          if (all.hasNext)
            all.next
          else {
            if (waitTime > 0)
              synchronized {
                beforeWaitFn
                wait(waitTime)
              }
            all = getAll
            next
          }
        } catch {
          case e:Exception =>
            e.printStackTrace()
            all = getAll
            next
        }
      }
    }
}